<?php

namespace app\admin\controller\oa;

use app\common\controller\Backend;
use app\common\library\Auth;
use think\Db;
use think\Env;

/**
 * 我的OA
 *
 * @icon fa fa-user
 */
class My extends Backend
{


    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];
    protected $relationSearch = true;

    /**
     * @var \app\admin\model\User
     */
    protected $model = null;
    protected $models = null;
    protected $categorylist = [];
    private $redis;

    public function _initialize()
    {
        parent::_initialize();
        //$this->model = new \app\admin\model\Registration;
        $this->model = new \app\admin\model\Accepts;
        $this->redis = redis(Env::get('redis.host'), Env::get('redis.port'), Env::get('redis.pwd'), Env::get('redis.db'));
    }

    /**
     * 查看
     */
    public function index()
    {
        $three_day = date('Y-m-d', strtotime(" +2 day"));
        $seven_day = date('Y-m-d', strtotime(" +6 day"));
        $times = date('Y-m-d').' 00:00:00 - '.date('Y-m-d').' 23:59:59';
        $three = date('Y-m-d').' 00:00:00 - '.$three_day.' 23:59:59';
        $seven = date('Y-m-d').' 00:00:00 - '.$seven_day.' 23:59:59';


        $end_time = date('Y-m-d', strtotime('+3 month'));

        $notice = Db::table('fa_oa_relevance')
            ->alias('a')
            ->field('a.id,a.type,b.title')
            ->join('fa_oa_notice b','a.notice_id=b.id','left')
            ->where('a.admin_id',$this->auth->id)
            ->order('a.id','desc')
            ->limit(3)
            ->select();

        //预约今天人数
        $today_registration = Db::table('fa_registration')
            ->where(['date'=>date('Y-m-d'),'status'=>'待就诊'])
            ->count();

        //预约近3天人数
        $three_registration = Db::table('fa_registration')
            ->where(['date'=>['between time',[date('Y-m-d'),$three_day]],'status'=>'待就诊'])
            ->count();

        //预约近七天人数
        $seven_registration = Db::table('fa_registration')
            ->where(['date'=>['between time',[date('Y-m-d'),$seven_day]],'status'=>'待就诊'])
            ->count();

        //未补交欠款
        $arrearage = Db::table('fa_diagnosis_order')
            ->where('on_credit',1)
            ->count();

        //诊疗销售费用待录入
        $cost = Db::table('fa_diagnosis_order')
            ->where('entry',0)
            ->where('status','in','1,2')
            ->count();

        //药品过期提醒
        $past = Db::table('fa_storage')
            ->where('end_time','<=',$end_time)
            ->count();

        //库存预警
        $count = Db::table('fa_storage')
            ->where('quantity','<=',10)
            ->count();

        //日志
        $log = Db::table('fa_oa_relation')
            ->where(['admin_id'=>$this->auth->id,'status'=>1,'type'=>0])
            ->count();

        //物品申请
        $goods = Db::table('fa_oa_relation')
            ->where(['admin_id'=>$this->auth->id,'status'=>2,'type'=>0,'is_comment'=>0])
            ->count();

        //请假
        $leave = Db::table('fa_oa_relation')
            ->where(['admin_id'=>$this->auth->id,'status'=>3,'type'=>0,'is_comment'=>0])
            ->count();

        //出差
        $evection = Db::table('fa_oa_relation')
            ->where(['admin_id'=>$this->auth->id,'status'=>4,'type'=>0,'is_comment'=>0])
            ->count();

        //通用审批
        $universal = Db::table('fa_oa_relation')
            ->where(['admin_id'=>$this->auth->id,'status'=>5,'type'=>0,'is_comment'=>0])
            ->count();

        //客户维护
        $client = Db::table('fa_oa_relation')
            ->where(['admin_id'=>$this->auth->id,'status'=>11,'type'=>0,'is_comment'=>0])
            ->count();

        //报销
        $apply = Db::table('fa_oa_relation')
            ->where(['admin_id'=>$this->auth->id,'status'=>6,'type'=>0,'is_comment'=>0])
            ->count();

        //采购
        $purchase = Db::table('fa_oa_relation')
            ->where(['admin_id'=>$this->auth->id,'status'=>7,'type'=>0,'is_comment'=>0])
            ->count();

        //付款
        $payment = Db::table('fa_oa_relation')
            ->where(['admin_id'=>$this->auth->id,'status'=>8,'type'=>0,'is_comment'=>0])
            ->count();

        //员工内购申请
        $buy = Db::table('fa_oa_relation')
            ->where(['admin_id'=>$this->auth->id,'status'=>9,'type'=>0,'is_comment'=>0])
            ->count();

        //赠送服务申请
        $give = Db::table('fa_oa_relation')
            ->where(['admin_id'=>$this->auth->id,'status'=>10,'type'=>0,'is_comment'=>0])
            ->count();

        //用章申请
        $stamp = Db::table('fa_oa_relation')
            ->where(['admin_id'=>$this->auth->id,'status'=>12,'type'=>0,'is_comment'=>0])
            ->count();

        $this->view->assign("notice", $notice);
        $this->view->assign("time", date('Y-m-d H:i:s'));
        $this->view->assign("times", $times);
        $this->view->assign("three", $three);
        $this->view->assign("seven", $seven);
        $this->view->assign("seven_day", $seven_day);
        $this->view->assign("end_time", $end_time);
        $this->view->assign("today_registration", $today_registration);
        $this->view->assign("three_registration", $three_registration);
        $this->view->assign("seven_registration", $seven_registration);
        $this->view->assign("arrearage", $arrearage);
        $this->view->assign("cost", $cost);
        $this->view->assign("past", $past);
        $this->view->assign("count", $count);
        $this->view->assign("log", $log);
        $this->view->assign("goods", $goods);
        $this->view->assign("leave", $leave);
        $this->view->assign("evection", $evection);
        $this->view->assign("universal", $universal);
        $this->view->assign("client", $client);
        $this->view->assign("apply", $apply);
        $this->view->assign("purchase", $purchase);
        $this->view->assign("payment", $payment);
        $this->view->assign("buy", $buy);
        $this->view->assign("give", $give);
        $this->view->assign("stamp", $stamp);
        return $this->view->fetch();
    }


    /**
     * 详情
     */
    public function detail($ids)
    {
        $row = $this->model->get(['id' => $ids]);

        if (!$row) {
            $this->error(__('No Results were found'));
        }

        $info = Db::table('fa_info')
            ->alias('a')
            ->field('a.*,b.level,b.vip')
            ->join('fa_users b','a.user_id=b.id','left')
            ->where('a.id',$row->info_id)
            ->find();

        //诊疗单
        $diagnosiss = Db::table('fa_diagnosiss')
            ->where('order_id',$row->id)
            ->select();

        //药品单
        $drug = Db::table('fa_drug')
            ->where('order_id',$row->id)
            ->select();

        //会员服务单
        $vip = Db::table('fa_serve')
            ->where('order_id',$row->id)
            ->select();

        if (!empty($info)){
            $info['age'] = countage($info['birthday']);
            $level_name = Db::table('fa_vip_level')
                ->where('level',$info['level'])
                ->find();
            if (!empty($level_name)){
                $info['level_name'] = $level_name['level_name'];
            }else{
                $info['level_name'] = '';
            }

            $card_arr = Db::table('fa_vip_card')
                ->where('id',$info['vip'])
                ->find();

            if (!empty($card_arr)){
                $info['level_name'] = $level_name['level_name'].'('.$card_arr['name'].')';
            }

        }


        //1代表转账，2代表现金，3代表pos机，4代表支付宝，5代表微信，6代表储值卡，7代表积分
        switch ($row->pay){
            case 1:
                $row->pay = '转账';
                break;
            case 2:
                $row->pay = '现金';
                break;
            case 3:
                $row->pay = 'pos机';
                break;
            case 4:
                $row->pay = '支付宝';
                break;
            case 5:
                $row->pay = '微信';
                break;
            case 6:
                $row->pay = '储值卡';
                break;
            case 7:
                $row->pay = '积分';
                break;
            default:
                $row->pay = '无';
                break;
        }

        $row->receivable = number_format($row->count_cost+$row->other_cost-$row->vip_discounts-$row->iscounts_cost-$row->vip_deduction,2,'.','');

        if ($row->on_credit == 2){
            $row->on_account = '0.00';
        }

        $this->view->assign("row", $row);
        $this->view->assign("info", $info);
        $this->view->assign("diagnosiss", $diagnosiss);
        $this->view->assign("drug", $drug);
        $this->view->assign("vip", $vip);
        return $this->view->fetch();
    }

}
